package spider.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import spider.model.Msg;
import spider.util.ConnectDB;

public class MsgDAO {
	private Connection conn;
	private PreparedStatement ps;
	public boolean isMsgExist(Msg m)
	{
		boolean bExist=false;
		try
		{
			conn=ConnectDB.getDBConnect();
			String sql="select uid from tb_msg where uid=? AND title=?";
			ps=conn.prepareStatement(sql);
			ps.setInt(1, m.getSid());
			ps.setString(2, m.getTitle());
			ResultSet rs=ps.executeQuery();
			bExist=rs.next();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return bExist;
	}
	public void writeMsgDB(List<Msg>msglist,int start,int end) throws Exception
	{
		if(msglist.isEmpty()||start==end)
			return;
		conn=ConnectDB.getDBConnect();
		String sql="insert tb_msg values(?,?,?,?,?)";
		ps=conn.prepareStatement(sql);
		//long date=Calendar.getInstance().getTimeInMillis();
		for(int i=start;i<end;++i)
		{
			Msg m=msglist.get(i);
			ps.setInt(1, 0);
			ps.setInt(2, m.getSid());
			ps.setString(3, m.getTitle());
			ps.setString(4, m.getContent());
			ps.setDate(5, null);
			ps.executeUpdate();
		}
		ps.close();
		conn.close();
	}
}
